<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" id="file" name="img">
    <img src="" id="img" alt="">
    <input type="submit" value="上传">
  </form>
  <!-- files -->
  <script>
    var file = document.querySelector('#file');
    var oImg = document.querySelector('#img');
    file.onchange = function(){
      console.log(this.files[0]); // blob 转换base64
      var fd = new FileReader();
      fd.readAsDataURL(this.files[0]);
      fd.onload = function(){
        // fd转换文件是异步的 onload事件 监听转换 进度，转换完成后触发，将结果放到fd.result属性
        console.log(this.result);
        oImg.src = this.result;
      }
    }
  </script>
</body>
</html>